Once you obtain a pointer to a sampled sound header, you can use the bufferCmd sound command to play a sound without using the high-level Sound Manager routines. Many sampled-sound resources include bufferCmd commands, so the high-level Sound Manager routines often issue the bufferCmd command indirectly. Thus, you might in some cases be able to make your application slightly more efficient by issuing the bufferCmd command directly. Also, you might issue a bufferCmd command directly if you want the Sound Manager to ignore other parts of a sound resource.
Finally, you might issue bufferCmd commands directly if you want your application to be able to play a large sound resource without loading the entire resource at once. By issuing several successive bufferCmd commands, you can play a large sound resource using a small buffer. In this case, each buffer must contain a sampled sound header. In most cases, the sound will play smoothly, without audible gaps. It's generally easier, however, to play large sampled sounds from disk by using the play-from-disk routines or the SndPlayDoubleBuffer function. See "Managing Double Buffers" for complete details.
Using the bufferCmd command to play several consecutive compressed samples on the Macintosh Plus, the Macintosh SE, or the Macintosh Classic is not guaranteed to work without an audible pause or click.
The pointer in the param2 field of a bufferCmd command is the location of a sampled sound header. A bufferCmd command is queued in the channel until the preceding commands have been processed. If the bufferCmd command is contained within an 'snd ' resource, the high bit of the command must be set. If the sound was loaded in from an 'snd ' resource, your application is expected to unlock this resource and allow it to be purged after using it. Listing 1-40 shows how your application can play a sampled sound stored in a resource using the bufferCmd command.
Listing 40 Playing a sound using the bufferCmd command
FUNCTION MyLowLevelSampledSndPlay (chan: SndChannelPtr; sndHandle: Handle):
OSErr;
CONST
kWaitIfFull = TRUE; {wait for room in queue?}
VAR
mySndHeader: SoundHeaderPtr;
mySndCmd: SndCommand; {a sound command}
BEGIN
mySndHeader := MyGetSoundHeader(sndHandle);
WITH mySndCmd DO
BEGIN
cmd := bufferCmd; {command is bufferCmd}
param1 := 0; {unused with bufferCmd}
param2 := LongInt(mySndHeader); {pointer to sound header}
END;
IF mySndHeader <> NIL THEN
MyLowLevelSampledSndPlay :=
SndDoCommand(chan, mySndCmd, NOT kWaitIfFull)
ELSE
MyLowLevelSampledSndPlay := badFormat;
END;
For the MyLowLevelSampledSndPlay function defined in Listing 1-40 to play a sound, the channel passed to it must already be configured to play sampled-sound data. Otherwise, the function returns a badChannel result code. Also, because the bufferCmd command works asynchronously, you might want to associate a callback procedure with the sound channel when you create the channel. For more information on playing sounds asynchronously, see "Playing Sounds Asynchronously" .
You can use the bufferCmd command to handle compressed sound samples in addition to sounds that are not compressed. To expand and play back a buffer of compressed samples, you pass the Sound Manager a bufferCmd command where param2 points to a compressed sound header.
To play sampled sounds that are not compressed, pass bufferCmd a standard or extended sound header. The extended sound header can be used for stereo sampled sounds. The standard sampled sound header is used for all other noncompressed sampled sounds.
| Previous | Chapter contents | Chapter top | Section top | Next |